(function( $ ){

    var methods = {
		init : function(options) {
		  
		  var settings =   $.extend( {
		      clean:true,
		      toggle: undefined
		  }, options);
		  
		  $(this).change(function(){handle_change($(this),settings)});
		  handle_change($(this),settings);
		  return this;
	  }
	  	
    };
    
    $.fn.disable = function(method){
    	// Method calling logic
        if ( methods[method] ) {
          return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } else if ( typeof method === 'object' || ! method ) {
          return methods.init.apply( this, arguments );
        } else {
          $.error( 'Method ' +  method + ' does not exist on disable' );
        }  
    };
  
  
})( jQuery );

function handle_change(element,settings){
	var disable;
	disable = settings.toggle == undefined? default_toggle(element): settings.toggle();
	
	if(disable){
		disable_area(settings);
	}else{
		enable_area(settings);
	}
}

function default_toggle(element){
	return $(element).val() != 0;
}

function disable_area(settings){
	var container_selector = settings.target;
	$(container_selector+' input,'+container_selector+' select').animate({opacity:0.5},{duration:500, complete:function(){
		$(container_selector+' input').attr('readonly','readonly');
		$(container_selector+' select').attr('disabled','true');
		if(settings.clean){
			$(container_selector+' input').val(undefined);
			$(container_selector+' select').val(undefined);
		}
	}});
}

function enable_area(settings){
	var container_selector = settings.target;
	$(container_selector+' input,'+container_selector+' select').animate({opacity:1},{duration:500, complete:function(){
		$(container_selector+' input').removeAttr('readonly');
		$(container_selector+' select').removeAttr('disabled');
	}});
	
}